package chloroform

import scaladuct.ConfigPage
import java.lang.String
import util.RequestUtils
import javax.servlet.http.{HttpSession, HttpServletRequest}

/**
 * <p>
 * Created by IntelliJ IDEA.
 * User: zslajchrt
 * Date: Feb 17, 2011
 * Time: 7:44:33 PM
 */
object ConfigPageRegisterLookup {

  def get(session: HttpSession): ConfigPageRegisterLookup = {
    val lookupFactory = session.getAttribute(classOf[ConfigPageRegisterLookup].getName).asInstanceOf[ConfigPageRegisterLookup]
    require(lookupFactory != null, "No lookup factory installed")
    lookupFactory
  }

  def getConfigPageRegister(projectName: String, request: HttpServletRequest): ConfigPageRegister = {
    get(request.getSession).getConfigPageRegister(projectName, request)
  }


  def getConfigPageRegister(request: HttpServletRequest): ConfigPageRegister = {
    get(request.getSession).getConfigPageRegister(request)
  }

  def apply[X](request: HttpServletRequest): ConfigPage[X] = {
    apply[X](request, resolvePageName(request))
  }

  def resolvePageName(request: HttpServletRequest) = {
    RequestUtils.resolvePageName(request)
  }

  def apply[X](request: HttpServletRequest, pageName: String): ConfigPage[X] = {
    val register = get(request.getSession).getConfigPageRegister(request)
    register.getConfigPage[X](pageName)
  }
}

trait ConfigPageRegisterLookup {
  def getConfigPageRegister(projectName: String, request: HttpServletRequest): ConfigPageRegister

  def getConfigPageRegister(request: HttpServletRequest): ConfigPageRegister

  def getProjectCache(): ProjectCache

  def getProjectName(): String

  def getMetaProjectName(): String

  def getProjectRef(): ProjectRef

  def getMetaProjectRef(): ProjectRef

  def getScenarios(): Array[String]

  def getMetaScenarios(): Array[String]

  def getMetaRegister(): MetaConfigPageRegister

  def getMetaMetaRegister(): MetaConfigPageRegister

  def getResourceResolver(): ResourceResolver

  def getProjectLoader(): ProjectLoader
}
